import './role.less'
import { transTreeData } from '@/utils'

import layer from '@/components/layer'
import permission from '@/components/permission'

export default {
    data() {
        return {
            roleDefaultProps: {
                label: 'name',
                children: 'childs'
            },
            // 当前数据
            currentData: {
                id: 0,
                name: ''
            },
            // 编辑layer数据
            editLayerData: {
                show: false,
                title: '添加',
                type: 0,          // 0:添加  1:修改  2:添加子级
                error: false,
                name: ''
            },
            // 删除layer数据
            deleteLayerData: {
                id: 0,
                show: false,
                title: '删除',
                tips: ''
            },
            // 角色资源
            roleResource: [],
            resourceDefaultCheckedId: [],
            resourceDefaultProps: {
                children: 'children',
                label: 'name'
            }
        }
    },
    computed: {
        roleList() {
            return this.$store.state.system.roleList
        },
        treeRoleResource() {
            return transTreeData({ list: this.roleResource })
        }
    },
    components: {
        layer,
        permission
    },
    mounted() {
        this.$store.dispatch('setBreadData', [
            {
                name: '系统管理'
            },
            {
                name: '角色管理'
            }
        ])
        if (this.$store.state.system.roleList && this.$store.state.system.roleList.length == 0) {
            this.$store.dispatch('getRoleList')
        }
    },
    methods: {
        //  关闭编辑layer
        closeEditLayer() {
            this.editLayerData.name = ''
            this.editLayerData.error = false
        },
        // 显示编辑layer
        showEditLayer(type) {
            this.editLayerData.show = true
            this.editLayerData.type = type
            if (type == 0) {
                this.editLayerData.title = '添加角色'
            }
            else if (type == 1) {
                this.editLayerData.title = '修改角色'
                this.editLayerData.name = this.currentData.name
            }
            else if (type == 2) {
                this.editLayerData.title = '添加子角色'
                this.editLayerData.name = ''
            }
        },
        // 输入框失焦
        layerInputBlur() {
            if (!this.editLayerData.name) {
                this.editLayerData.error = true
            }
            else {
                this.editLayerData.error = false
            }
        },
        // 编辑提交
        editLayerConfirm() {
            if (!this.editLayerData.name) {
                this.editLayerData.error = true
            }
            else {
                let _type = this.editLayerData.type
                let _param = {
                    type: _type,
                    data: {
                        name: this.editLayerData.name
                    }
                }
                if (_type == 1) {
                    _param.data.id = this.currentData.id
                }
                else if (_type == 2) {
                    _param.data.parentId = this.currentData.id
                }
                api.editRole(_param).then(res => {
                    if (_type == 0) {
                        this.$msg.success('添加角色成功')
                    }
                    else if (_type == 1) {
                        this.$msg.success('修改角色成功')
                    }
                    else if (_type == 2) {
                        this.$msg.success('添加子角色成功')
                    }
                    this.$store.dispatch('getRoleList')
                    this.editLayerData.show = false

                    if (_type == 1) {
                        this.currentData.name = this.editLayerData.name
                    }
                    else {
                        this.currentData.id = 0
                        this.currentData.name = ''
                    }
                }).catch(res => {
                    console.log(res)
                })
            }
        },
        // 显示删除layer
        showDeleteLayer() {
            this.deleteLayerData.show = true
            this.deleteLayerData.tips = '确认删除该角色名称？'
        },
        // 删除提交
        deleteLayerConfirm() {
            api.delRole({
                id: this.currentData.id
            }).then(res => {
                this.$msg.success('删除角色成功')
                this.deleteLayerData.show = false
                this.currentData.id = 0
                this.currentData.name = ''
                this.$store.dispatch('getRoleList')
            }).catch(res => {
                console.log(res)
            })
        },
        nodeClick(data) {
            this.currentData.id = data.id
            this.currentData.name = data.name
            this.getRoleResource(data.id)
        },
        getRoleResource(id) {
            this.roleResource = []
            api.getRoleResource({
                id: id
            }).then(res => {
                this.roleResource = res
                this.resourceDefaultCheckedId = []
                res.forEach((item) => {
                    if (item.roleResourceId) {
                        this.resourceDefaultCheckedId.push(item.id);
                    }
                })
            }).catch(res => {
                console.log(res)
            })
        },
        sumbitSave() {
            let _selectedCheckArray = this.$refs.treeResource.getCheckedKeys()
            let _param = []
            _selectedCheckArray.forEach(id => {
                let _obj = {};
                _obj.idRole = this.currentData.id
                _obj.idResource = id
                _param.push(_obj)

            })
            api.editDiction(_param).then(res => {
                this.$msg.success('修改角色权限成功')
            }).catch(res => {
                console.log(res)
            })
        }
    }
}